//
//  HMHomeViewController.m
//  localLife
//
//  Created by Limonwei on 15/11/23.
//  Copyright © 2015年 heima. All rights reserved.
//

#import "HMHomeViewController.h"
#import "HMLoginViewController.h"
#import "HMLocaltionManager.h"
#import <CoreAudioKit/CoreAudioKit.h>
#import "HMHomeModel.h"
#import "HMFocus.h"
#import "HMFocusTableViewCell.h"
#import "HMGroupTableViewCell.h"
#import "HMFamousTableViewCell.h"
#import "HMGuessTableViewCell.h"
#import "HMGoodsViewController.h"

@interface HMHomeViewController ()<UITableViewDataSource,UITableViewDelegate,HMGroupTableViewCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong)HMFocusTableViewCell *headerView;

@property(nonatomic,strong)HMHomeModel *model;

@end

@implementation HMHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self SHOWloadView];
    
    self.titltLabel.text = @"首页";
    
    [self performSelector:@selector(dismissLoadView) withObject:nil afterDelay:2];
    
   // [self showToast:@"xxx"];
    
    self.leftButton.hidden = YES;
    
    [self loadData];
    
    [HMLocaltionManager localSuccess:^(CLLocationCoordinate2D coordinate) {
        //
        DSLog(@"%f",coordinate.longitude);
    } withError:^(NSError *error) {
        //
        DSLog(@"%@",error);
    }];
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    _headerView = [[[NSBundle mainBundle]loadNibNamed:@"HMFocusTableViewCell" owner:nil options:nil] objectAtIndex:0];
    
    _headerView.frame = CGRectMake(0, 0, kUIScreenWidth, (425-88)/2.0);
    
    self.tableView.tableHeaderView = _headerView;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}


-(void)login{
    
    HMLoginViewController *login = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"HMLoginViewController"];
    
    AppDelegate *app =(AppDelegate *) [UIApplication sharedApplication].delegate;
    
    [app.tabbar showOrHiddenTabbarView:NO];
    
    [self.navigationController pushViewController:login animated:YES];
    
}


//一定要打印参数 咨询后台
-(void)loadData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    [params setValue:@"120.38" forKey:@"lon"];
    
    [params setValue:@"36.06" forKey:@"lat"];
    
    [params setValue:[HMFunction getTime] forKey:@"time"];
    
    NSArray *temp = @[APP_ID,params[@"lon"],params[@"lat"],params[@"time"],APP_IDENTIFER];
    
    [params setValue:[HMFunction signMD5String:temp] forKey:@"sign"];
    
    [params setObject:APP_ID forKey:@"app_id"];
    
    
    DSLog(@"%@",params);
    
    [HMAFHTTPClient getHomeWithParams:params withSuccessBlock:^(HMHomeModel *model) {
        //
        DSLog(@"%@",model);
        
        _headerView.model = model;
        
        _model = model;
        
        [_tableView reloadData];
        
   
    } withFailedBlock:^(NSError *error) {
        //
        
        DSLog(@"%@",error);
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0 || section == 1) {
        return 1;
    }else if(section == 2){
        
        return _model.guess.list.count;
        
    }else
        return 0;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        static NSString *ID = @"HMGroupTableViewCell";
        
        HMGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [[[UINib nibWithNibName:ID bundle:nil]instantiateWithOwner:self options:nil]objectAtIndex:0];
        }
        if (_model != nil) {
            cell.list = _model.group;
            
            cell.delegate = self;
        }
        
        return cell;
    }else if (indexPath.section == 1){
        
        static NSString *ID = @"HMFamousTableViewCell";
        
        HMFamousTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [[[UINib nibWithNibName:ID bundle:nil]instantiateWithOwner:self options:nil]objectAtIndex:0];
        }
        if (_model != nil) {
            cell.list = _model.famous;
            
            
        }
        
        return cell;

    }else if (indexPath.section == 2){
        
        static NSString *ID = @"HMGuessTableViewCell";
        
        HMGuessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [[[UINib nibWithNibName:ID bundle:nil]instantiateWithOwner:self options:nil]objectAtIndex:0];
        }
        if (_model != nil) {
            cell.model = _model.guess.list[indexPath.row];
            
           
        }
        
        return cell;

        
    }else
        
        return NULL;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return 180;
    }else if (indexPath.section == 1){
        
        return 200;
    }else
        return 120;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0;
    }else
        
        return kUITableViewHeightForHeader;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUITableViewHeightForHeader)];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 5)];
    
    line.backgroundColor = [UIColor yellowColor];
    
    [view addSubview:line];
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
    
    title.textColor = [UIColor colorWithRed:233/255.0 green:76/255.0 blue:80/255.0 alpha:1.0];
    
    [view addSubview:title];
    
    UIView *sep = [[UIView alloc]initWithFrame:CGRectMake(0, kUITableViewHeightForHeader-0.5, kUIScreenWidth, 0.5)];
    
    sep.backgroundColor = [UIColor blackColor];
    [view addSubview: sep];
    
    if (section == 1) {
        title.text = @"名店推荐";
    }else if (section == 2){
        title.text = @"猜你喜欢";
    }
    
    return view;
    
}

- (void)didItemSelected:(NSInteger)index{
    
    NSLog(@"我点击了第%ld个",index);
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    HMGoodsViewController *goods = [sb instantiateViewControllerWithIdentifier:@"HMGoodsViewController"];
    
    
    
    [self.navigationController pushViewController:goods animated:YES];
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [app.tabbar showOrHiddenTabbarView:YES];
     
}


@end





















